package com.adopapa.nezhacloud.ddd.codegen;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import jakarta.persistence.Lob;

public final class GenCodeUtil {

	public static <T> List<Map<String, Object>> getQueryFieldMap(Class<T> clazz) {
		List<Map<String, Object>> results = new LinkedList<>();
		Map<String, Object> fieldMap = null;
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			CodeGen codeGen = field.getAnnotation(CodeGen.class);
			if (codeGen != null && codeGen.query().length() > 0) {
				fieldMap = new HashMap<String, Object>();
				fieldMap.put("label", codeGen.label());
				fieldMap.put("query", codeGen.query());
				fieldMap.put("unique", codeGen.unique());
				fieldMap.put("time", codeGen.time());
				fieldMap.put("required", codeGen.required());
				fieldMap.put("fieldName", field.getName());
				fieldMap.put("FieldName",
						field.getName().substring(0, 1).toUpperCase().concat(field.getName().substring(1)));
//				if (codeGen.time()) {
//					fieldMap.put("type", "String");
//				} else { // java.lang.
//					fieldMap.put("type", field.getGenericType().getTypeName().substring(10));
//				}

				if (codeGen.type().equals(CodeGen.TYPE_CLASS)) {
					if (field.getGenericType().getTypeName().indexOf("$") > 0) {
						fieldMap.put("type", field.getGenericType().getTypeName()
								.substring(field.getGenericType().getTypeName().lastIndexOf("$") + 1));
					} else {
						fieldMap.put("type", field.getGenericType().getTypeName()
								.substring(field.getGenericType().getTypeName().lastIndexOf(".") + 1));
					}
				} else if (codeGen.type().equals(CodeGen.TYPE_ENUM)) {
					fieldMap.put("type", "String");
				} else {
					fieldMap.put("type", codeGen.type());
				}

				results.add(fieldMap);
			}
		}

		return results;
	}

	public static <T> List<Field> getFormFields(Class<T> clazz) {
		List<Field> results = new LinkedList<Field>();

		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			CodeGen codeGen = field.getAnnotation(CodeGen.class);
			if (codeGen != null && codeGen.form()) {
				results.add(field);
			}
		}

		return results;
	}

	public static <T> List<Map<String, Object>> getFormFieldMap(Class<T> clazz) {
		List<Map<String, Object>> results = new LinkedList<>();
		Map<String, Object> fieldMap = null;
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			CodeGen codeGen = field.getAnnotation(CodeGen.class);
			if (codeGen != null && codeGen.form()) {
				fieldMap = new HashMap<String, Object>();
				fieldMap.put("label", codeGen.label());
				fieldMap.put("value", codeGen.value());
				fieldMap.put("fieldName", field.getName());
				fieldMap.put("FieldName",
						field.getName().substring(0, 1).toUpperCase().concat(field.getName().substring(1)));
				fieldMap.put("math", codeGen.math());
				fieldMap.put("unique", codeGen.unique());
				fieldMap.put("required", codeGen.required());
				if (codeGen.math()) {
					fieldMap.put("type", "Double");
				} else { // java.lang.
					if (codeGen.type().equals(CodeGen.TYPE_CLASS)) {
						if (field.getGenericType().getTypeName().indexOf("$") > 0) {
							fieldMap.put("type", field.getGenericType().getTypeName()
									.substring(field.getGenericType().getTypeName().lastIndexOf("$") + 1));
						} else {
							fieldMap.put("type", field.getGenericType().getTypeName()
									.substring(field.getGenericType().getTypeName().lastIndexOf(".") + 1));
						}
					} else if (codeGen.type().equals(CodeGen.TYPE_ENUM)) {
						fieldMap.put("type", "String");
					} else {
						fieldMap.put("type", codeGen.type());
					}
					Lob lob = field.getAnnotation(Lob.class);
					fieldMap.put("text", false);
					if (lob != null) {
						fieldMap.put("text", true);
					} else {
					}
				}
				results.add(fieldMap);
			}
		}

		return results;
	}

	public static String getCamelName(String clazzName) {
		return clazzName.substring(0, 1).toLowerCase().concat(clazzName.substring(1));
	}

	public static String camel2Snake(String camelStr) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < camelStr.length(); i++) {
			if (Character.isUpperCase(camelStr.charAt(i))) {
				sb.append("_");
				sb.append(camelStr.charAt(i));
			} else {
				sb.append(camelStr.charAt(i));
			}
		}

		if (sb.toString().startsWith("_")) {
			return sb.substring(1).toLowerCase();
		}

		return sb.toString().toLowerCase();

	}

	public static String snake2Camel(String snakeStr) {
		StringBuilder sb = new StringBuilder();
		String[] splits = snakeStr.split("_");
		for (int i = 0; i < splits.length; i++) {
			if (i == 0) {
				sb.append(splits[i]);
			} else {
				sb.append(splits[i].substring(0, 1).toUpperCase()).append(splits[i].substring(1));
			}
		}

		return sb.toString();
	}

}
